home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 21 Ambient Occlusion / Ssao / Shaders / Default.hlsl < prev    next >
Encoding:
Text File  |  2016-03-02  |  4.3 KB  |  135 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include common HLSL code.
  19. #include "Common.hlsl"
  20.  
  21. struct VertexIn
  22. {
  23.     float3 PosL    : POSITION;
  24.     float3 NormalL : NORMAL;
  25.     float2 TexC    : TEXCOORD;
  26.     float3 TangentU : TANGENT;
  27. };
  28.  
  29. struct VertexOut
  30. {
  31.     float4 PosH    : SV_POSITION;
  32.     float4 ShadowPosH : POSITION0;
  33.     float4 SsaoPosH   : POSITION1;
  34.     float3 PosW    : POSITION2;
  35.     float3 NormalW : NORMAL;
  36.     float3 TangentW : TANGENT;
  37.     float2 TexC    : TEXCOORD;
  38. };
  39.  
  40. VertexOut VS(VertexIn vin)
  41. {
  42.     VertexOut vout = (VertexOut)0.0f;
  43.  
  44.     // Fetch the material data.
  45.     MaterialData matData = gMaterialData[gMaterialIndex];
  46.     
  47.     // Transform to world space.
  48.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  49.     vout.PosW = posW.xyz;
  50.  
  51.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  52.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  53.     
  54.     vout.TangentW = mul(vin.TangentU, (float3x3)gWorld);
  55.  
  56.     // Transform to homogeneous clip space.
  57.     vout.PosH = mul(posW, gViewProj);
  58.  
  59.     // Generate projective tex-coords to project SSAO map onto scene.
  60.     vout.SsaoPosH = mul(posW, gViewProjTex);
  61.     
  62.     // Output vertex attributes for interpolation across triangle.
  63.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  64.     vout.TexC = mul(texC, matData.MatTransform).xy;
  65.  
  66.     // Generate projective tex-coords to project shadow map onto scene.
  67.     vout.ShadowPosH = mul(posW, gShadowTransform);
  68.     
  69.     return vout;
  70. }
  71.  
  72. float4 PS(VertexOut pin) : SV_Target
  73. {
  74.     // Fetch the material data.
  75.     MaterialData matData = gMaterialData[gMaterialIndex];
  76.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  77.     float3 fresnelR0 = matData.FresnelR0;
  78.     float  roughness = matData.Roughness;
  79.     uint diffuseMapIndex = matData.DiffuseMapIndex;
  80.     uint normalMapIndex = matData.NormalMapIndex;
  81.     
  82.     // Dynamically look up the texture in the array.
  83.     diffuseAlbedo *= gTextureMaps[diffuseMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  84.  
  85. #ifdef ALPHA_TEST
  86.     // Discard pixel if texture alpha < 0.1.  We do this test as soon 
  87.     // as possible in the shader so that we can potentially exit the
  88.     // shader early, thereby skipping the rest of the shader code.
  89.     clip(diffuseAlbedo.a - 0.1f);
  90. #endif
  91.  
  92.     // Interpolating normal can unnormalize it, so renormalize it.
  93.     pin.NormalW = normalize(pin.NormalW);
  94.     
  95.     float4 normalMapSample = gTextureMaps[normalMapIndex].Sample(gsamAnisotropicWrap, pin.TexC);
  96.     float3 bumpedNormalW = NormalSampleToWorldSpace(normalMapSample.rgb, pin.NormalW, pin.TangentW);
  97.  
  98.     // Uncomment to turn off normal mapping.
  99.     //bumpedNormalW = pin.NormalW;
  100.  
  101.     // Vector from point being lit to eye. 
  102.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  103.  
  104.     // Finish texture projection and sample SSAO map.
  105.     pin.SsaoPosH /= pin.SsaoPosH.w;
  106.     float ambientAccess = gSsaoMap.Sample(gsamLinearClamp, pin.SsaoPosH.xy, 0.0f).r;
  107.  
  108.     // Light terms.
  109.     float4 ambient = ambientAccess*gAmbientLight*diffuseAlbedo;
  110.  
  111.     // Only the first light casts a shadow.
  112.     float3 shadowFactor = float3(1.0f, 1.0f, 1.0f);
  113.     shadowFactor[0] = CalcShadowFactor(pin.ShadowPosH);
  114.  
  115.     const float shininess = (1.0f - roughness) * normalMapSample.a;
  116.     Material mat = { diffuseAlbedo, fresnelR0, shininess };
  117.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  118.         bumpedNormalW, toEyeW, shadowFactor);
  119.  
  120.     float4 litColor = ambient + directLight;
  121.  
  122.     // Add in specular reflections.
  123.     float3 r = reflect(-toEyeW, bumpedNormalW);
  124.     float4 reflectionColor = gCubeMap.Sample(gsamLinearWrap, r);
  125.     float3 fresnelFactor = SchlickFresnel(fresnelR0, bumpedNormalW, r);
  126.     litColor.rgb += shininess * fresnelFactor * reflectionColor.rgb;
  127.     
  128.     // Common convention to take alpha from diffuse albedo.
  129.     litColor.a = diffuseAlbedo.a;
  130.  
  131.     return litColor;
  132. }
  133.  
  134.  
  135.